package
{
	import UIAway.uif_view;
	
	import UIFree.uif_app;
	import UIFree.uif_styles;
	
	import away3d.lights.PointLight;
	import away3d.materials.ColorMaterial;
	import away3d.materials.MaterialBase;
	import away3d.primitives.Cube;
	import away3d.primitives.Sphere;
	import away3d.primitives.WireframeCube;
	import away3d.vf.HoverDragController;
	
	import flash.events.Event;
	import flash.geom.Matrix3D;
	import flash.geom.Vector3D;
	
	import jiglib.debug.Stats;
	import jiglib.physics.RigidBody;
	import jiglib.plugin.away3d4.Away3D4Physics;
	
	import mx.containers.Box;
	
	[SWF(width="1000", height="500", frameRate="120", backgroundColor="#FFFFFF")]
	public class phisicExp extends uif_app {
		private var _view		: uif_view;
		private var _lights		: Array;
		private var _sphere		: RigidBody;
		private var _box		: RigidBody;
		private var _physic		: Away3D4Physics;
		
		public function phisicExp(a_styles:uif_styles=null) {
			super(a_styles);
//			styleClass = 'black';
			resize(stage.stageWidth, stage.stageHeight);
			
			_view = new uif_view(this, 'data/textures/env/19_%d.jpg');
			_view.view.camera.position = new Vector3D(0, 0, -400);
			
			_view.lights = createLights();
			createPhysic();
			createControls();
			createShapes();
			listenEvents();
			
			addChild(new Stats(_view.view, _physic));
		}
		
		protected function listenEvents(): void {
			addEventListener(Event.ENTER_FRAME, doFrame);
		}
		
		protected function createLights(): Array {
			var _light: PointLight = new PointLight();
			_light.diffuse = 1;
			_light.specular = 1;
			_light.x = -1000;
			_light.y = 1000;
			_light.z = -1000;
			_light.color = 0xffeeFF;
			
			var _light2: PointLight = new PointLight();
			_light2.x = 1000;
			_light2.y = -1000;
			_light2.z = 1000;
			_light2.color = 0x662233;
			
			_lights = [_light, _light2];
			return _lights;
		}
		
		protected function createControls(): void {
			(new HoverDragController(_view)).radius = 500;
		}
		
		protected function createShapes(): void {
			var mat: MaterialBase = new ColorMaterial(0xFF0000);
			var matOpacity: MaterialBase = new ColorMaterial(0x000000, 0.3);
			mat.lights = _view.lights;
			matOpacity.lights = _view.lights;
			matOpacity.bothSides = true;
			
			_sphere = _physic.createSphere(mat, 20);
			//_sphere.addBodyForce(new Vector3D(100, 0, 0), _sphere.currentState.position);
			//_sphere.setLineVelocity(new Vector3D(10, 0, 0));
		//	_sphere.addWorldForce(new Vector3D(10, 0, 0), _sphere.currentState.position);
//			_sphere.applyWorldImpulse(new Vector3D(0, -50, 0), _sphere.currentState.position);
			
			_box = _physic.createCube(matOpacity, 300, 20, 300);
			_box.moveTo(new Vector3D(0, -200, 0));
			_box.rotationZ = 10;
			_box.movable = false;
			//_box.addBodyForce(new Vector3D(0, 20, 0), _box.currentState.position, true);
/*			
			_view.view.scene.addChild(_box);
			_view.view.scene.addChild(_sphere);*/
		}
		
		protected function createPhysic(): void {
			_physic = new Away3D4Physics(_view.view);
//			_physic.engine.setGravity(new Vector3D());
		}
		
		protected function doFrame(e: Event): void {
			_physic.step(0.2);
		}
	}
}